package yunhe.service.impl;

import org.apache.ibatis.session.SqlSession;
import yunhe.dao.BoardDao;
import yunhe.entity.BoardInfo;
import yunhe.service.BoardServers;
import yunhe.util.SqlSessionFactoryUtil;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BoardServersImpl implements BoardServers {
	
	public static SqlSession ss=SqlSessionFactoryUtil.getSqlSession();
	public static BoardDao bd=ss.getMapper(BoardDao.class);
	
	public List<BoardInfo> getBoard() {

		List<BoardInfo> lbi;
		try {
			lbi=bd.getBoard();
		} catch (Exception e) {
			lbi=null;
		}
		return lbi;
	}
	
	public int updateBoard(Map<Integer, String> bid_bname) {

		Iterator<Integer> it=bid_bname.keySet().iterator();
		int re=-1;
		while (it.hasNext()) {
		    int key=it.next();
			re=bd.updateBoard(key, bid_bname.get(key));
		}
		if(re>0){
			ss.commit();
		}
		else{
			ss.rollback();
		}
		return re;
	}


	public int delBoard(int[] bids) {
		
		return bd.delBoard(bids);
	}

	
	public int addBoard(String[] bnames) {
		int re=-1;
		for(String bname:bnames){
            re=bd.addBoard(bname);
		}
        if(re>0){
            ss.commit();
        }
        else{
            ss.rollback();
        }
		return re;
	}
	
	

}
